/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.modules.user.controller;


import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.UserOrderLogEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.service.UserAmountService;
import io.renren.modules.sys.service.UserOrderLogService;

/**
 * 会员
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年10月31日 上午10:40:10
 */
@RestController
@RequestMapping("/user")
public class UserController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private UserOrderLogService userOrderLogService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private UserAmountService userAmountService;
	
	/**
	 * 所有用户列表
	 */
	@RequestMapping("/userlist")
	public R list(@RequestParam Map<String, Object> params){
		params.put("parentId", getUser().getUserId());
		PageUtils page = sysUserService.queryUserPage(params);

		return R.ok().put("page", page);
	}
	
	/**
	 * 获取登录的用户信息
	 */
	@RequestMapping("/info")
	public R info(){
		Map<String,Object> info = Maps.newHashMap();
		SysUserEntity param = getUser();
		param.setParentIds(param.getParentIds()+","+param.getUserId());
		int headCount = sysUserService.queryHeadCount(param);//邀请总人数
		
		param.setAuthFlag("0");
		int disableCount = sysUserService.queryHeadCount(param);//获取待激活人数
		
		int outAmount = userAmountService.queryUseAmount(param.getUserId());
		
		info.put("user", getUser());
		info.put("inAmount", (headCount - disableCount)*Constant.UNIT_PRICE);//总收入
		info.put("outAmount", outAmount);//总提现
		info.put("headCount", headCount);//邀请总人数
		info.put("disableCount", disableCount);//待激活人数
		
		return R.ok().put("info", info);
	}
	
	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{userId}")
	public R info(@PathVariable("userId") Long userId){
		SysUserEntity user = sysUserService.selectById(userId);
		
		//获取用户所属的角色列表
		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIdList(roleIdList);
		
		return R.ok().put("user", user);
	}

	
	/**
	 * 会员开户
	 */
	@SysLog("会员开户")
	@RequestMapping("/addUser")
	public R addUser(@RequestBody SysUserEntity user){
		String userId = String.valueOf(getUser().getUserId());
		user.setDeptId(getUser().getDeptId());
		user.setParentId(userId);
		user.setParentIds(getUser().getParentIds()+","+userId);
		user.setCreateBy(userId);
		user.setUpdateBy(userId);
		sysUserService.addUser(user);
		
		return R.ok();
	}
	
	/**
	 * 会员开户
	 */
	@SysLog("会员认证")
	@RequestMapping("/authUser")
	public R authUser(@RequestBody Long userId){
		sysUserService.authUser(userId);
		return R.ok();
	}
	
    /**
     * 提现列表
     */
	@SysLog("提现列表")
	@RequestMapping("/applylist")
    public R applylist(@RequestParam Map<String, Object> params){
    	params.put("userId", getUser().getUserId());
    	params.put("type", "0");
        PageUtils page = userOrderLogService.queryPage(params);

        return R.ok().put("page", page);
    }
	
	/**
	 * 会员开户
	 */
	@SysLog("提现申请")
	@RequestMapping("/addApply")
	public R addApply(@RequestBody UserOrderLogEntity order){
		String userId = String.valueOf(getUser().getUserId());
		order.setUserId(getUser().getUserId());
		order.setType("0");//提现申请
		order.setStatus("0");//待交易
		order.setCreateBy(userId);
		order.setUpdateBy(userId);
		order.setCreateTime(new Date());
		order.setUpdateTime(new Date());
		userOrderLogService.insert(order);
		return R.ok();
	}
	

}
